import Vue from 'vue'
import Vuex from 'vuex'
import $mConfig from '@/config/index.config.js'
import $mRoutesConfig from '@/config/routes.config.js'
import $mRouter from '@/common/router.js'

Vue.use(Vuex)

// uni.setStorageSync("token", "oVG1b1Bgrb");
// uni.setStorageSync("openId", "oVG1b1Bgrb-l6jNYCUr6sYpQ5F-U");

const TOKEN = uni.getStorageSync("token") || "";
const OPENID = uni.getStorageSync("openId") || "";
const USER_INFO = uni.getStorageSync("userInfo") || {};
const PERMISSION = uni.getStorageSync("permission") || {};
const DEFAULT_TABBAR=[{
				name: 'home',
				text: '首页',
				icon: '/static/icon/home_light.png',
				iconActive: '/static/icon/home_fill_light.png',
				path: '/pages/index/index'
			},
			{
				name: 'plan',
				text: '职位',
				icon: '/static/icon/search.png',
				iconActive: '/static/icon/search-fill.png',
				path: '/pages/type/type'
			},
			// {
			// 	name: 'plan',
			// 	text: '咨询',
			// 	icon: '/static/icon/ic_but_zhoye_nor.png',
			// 	iconActive: '/static/icon/ic_but_zhiye_sel.png',
			// 	path: '/pages/plan/plan'
			// },
			// {
			// 	name: 'train',
			// 	text: '指南',
			// 	icon: '/static/icon/ic_but_xiaoxi_nor.png',
			// 	iconActive: '/static/icon/ic_but_xiaoxi_sel.png',
			// 	path: '/pages/train/train'
			// },
			{
				name: 'chat',
				text: '消息',
				icon: '/static/icon/message_light.png',
				iconActive: '/static/icon/message_fill_light.png',
				info: 0,
				path: '/pages/chat/chat'
			},
			{
				name: 'mine',
				text: '我的',
				icon: '/static/icon/my_light.png',
				iconActive: '/static/icon/my_fill_light.png',
				path: '/pages/user/user'
			}
		];
	const DEFAULT_TABBAR2=[
					{
						name: 'home',
						text: '首页',
						icon: '/static/icon/home_light.png',
						iconActive: '/static/icon/home_fill_light.png',
						path: '/pages/index/index'
					},
					{
						name: 'plan',
						text: '职位',
						icon: '/static/icon/search.png',
						iconActive: '/static/icon/search-fill.png',
						path: '/pages/type/type'
					},
					// {
					// 	name: 'train',
					// 	text: '指南',
					// 	icon: '/static/icon/ic_but_xiaoxi_nor.png',
					// 	iconActive: '/static/icon/ic_but_xiaoxi_sel.png',
					// 	path: '/pages/train/train'
					// },
					{
						name: 'chat',
						text: '消息',
						icon: '/static/icon/message_light.png',
						iconActive: '/static/icon/message_fill_light.png',
						info: 0,
						path: '/pages/chat/chat'
					},
					{
						name: 'mine',
						text: '我的',
						icon: '/static/icon/my_light.png',
						iconActive: '/static/icon/my_fill_light.png',
						path: '/pages/user/user'
					}
			];

const store = new Vuex.Store({
	state: {
		// 是否強制登录
		forcedLogin: $mConfig.forcedLogin,
		// 前端token
		token: TOKEN,
		// 用户openid
		openId: OPENID,
		// 用户信息 头像 昵称
		userInfo: USER_INFO,
		//权限信息
		permission:PERMISSION,

		active: 'home',
		animate: 'zoomIn',
		tabbars: DEFAULT_TABBAR2,
	},
	getters: {
		// 用户是否登录
		hasLogin: state => {
			if (state.token) {
				return true;
			} else {
				return false
			}
		},
		active: state => state.active,
		animate: state => state.animate,
		tabbars: state => state.tabbars

	},
	mutations: {
		SET_TOKEN(state, token) {
			state.token = token;
			uni.setStorageSync("token", token);
		},
		SET_OPENID(state, openId) {
			state.openId = openId;
			uni.setStorageSync("openId", openId);
		},
		SET_USERINFO(state, userInfo) {
			state.userInfo = userInfo;
			uni.setStorageSync("userInfo", userInfo);
		},
		SET_CUSTOMERINFO(state, customerInfo) {
			state.customerInfo = customerInfo;
			uni.setStorageSync("customerInfo", customerInfo);
		},
		SET_LOGOUT(state) {
			state.token = "";
			uni.setStorageSync("token", "");
			uni.setStorageSync("openId", "");
			uni.setStorageSync("userInfo", "");
			uni.setStorageSync("permission", "");
			state.userInfo = "";
		},

		SET_ACTIVE(state, data) {
			state.active = data
		},
		SET_TABBARS(state, data) {
			//data转换
			let newData=[];
			for (let item of data) {
				for (let item2 of DEFAULT_TABBAR) {
					if (item.path==item2.path) {
						newData.push(item2);
					}
				}
			}
			state.tabbars = newData
		},
		
		INIT_TABBARS(state) {
			//data转换
			if(state.permission){
				let newData=[];
				let data=state.permission;
				for (let item of data) {
					for (let item2 of DEFAULT_TABBAR) {
						if (item.path==item2.path) {
							newData.push(item2);
						}
					}
				}
				state.tabbars = newData
			}
		},
		
		SET_TABBAR_INFO(state, data) {
			for (let i=0;i<state.tabbars.length;i++) {
				if(data.name==state.tabbars[i].name){
					state.tabbars[i].info=data.info;
				}
			}
		},
		
		SET_PERMISSION(state, data) {
			state.permission = data;
			uni.setStorageSync("permission", data);
		},
		
	},
	actions: {
		// 登录过期 重新登录
		reLogin({
			commit
		}, info) {
			commit("SET_TOKEN", "");
			commit("SET_USERINFO", "");
			// $mRouter.redirectTo({
			// 	route:$mRoutesConfig.login
			// });
			// #ifdef MP-WEIXIN || MP-MP-TOUTIAO
			$mRouter.push({
				route: $mRoutesConfig.login
			});
			// #endif
			// #ifndef MP-WEIXIN
			$mRouter.push({
				route: $mRoutesConfig.loginPwd
			});
			// #endif
		}
	}
})

export default store